<?php

/*
 * 获取SKU的相关信息
 */

class service_skudetail extends Basic
{
    //获取SKU的图片信息
    public static function sku_images($sku)
    {
        //START      --------------------------------------CWY修改代码------------------------------------
        $db = Skusystemapi::getSkuImage($sku);
        if(empty($db)){
            $db = DB::select('erp_images.filename')
                ->from('erp_images')
                ->join('erp_products_data')
                ->on('erp_images.proid','=','erp_products_data.products_id')
                ->where('erp_products_data.products_sku','=',$sku)
                ->execute('readonlyerp')
                ->current();
        }
        //END      --------------------------------------CWY修改代码------------------------------------
        return $db;
    }

    //获取SKU的供应商信息
    public static function sku_providers($sku)
    {
        //      --------------------------------------kdw------------------------------------
        //		$providers  = DB::select('erp_provider.vProviderName','erp_providerproducts.*')
        //		->from('erp_provider')
        //		->join('erp_providerproducts')
        //		->on('erp_provider.id','=','erp_providerproducts.iproviderID')
        //		->where('erp_providerproducts.iproductID','LIKE',trim($sku))
        //		->where('erp_provider.state','=',2)//2是审核通过的
        //		->order_by('erp_providerproducts.price')
        //		->execute('readonlyerp')
        //		->as_array();

        $providers = Newerpclientapi::querySupplierSkuJoin($sku);
        //      --------------------------------------kdw-end--------------------------------
        return $providers;
    }

    public static function load_sku_info_all($sku)
    {
        $sku_detailed             = service_skudetail::load_sku_info($sku); //得到SKU的基本信息
        $sku_images               = service_skudetail::sku_images($sku); //得到SKU的图片
        $sku_detailed['sku']      = $sku;
        $sku_detailed['filename'] = $sku_images['filename'];

        return $sku_detailed;
    }
    //获取图片地址功能
    //获取SKU的图片信息
    public static function get_erp_images($sku)
    {
        $image = '';
        //START      --------------------------------------CWY修改代码------------------------------------
        $img_arrs = Skusystemapi::getSkuImage($sku);
        if(empty($img_arrs)){
            $sql = "SELECT products_id,products_imagess FROM erp_products_data WHERE `products_sku`='".$sku."'";
            $pro = DB::query(Database::SELECT,$sql)
                ->execute('readonlyerp')
                ->current();
            //缩略图
            $thumbnail     = '';
            if($pro&&isset($pro['products_id'])&&$pro['products_id'])
            {
                $db_images_sql = "select * from erp_images where proid = ".$pro['products_id']; 
                $img_arrs      = DB::query(Database::SELECT,$db_images_sql)
                    ->execute('readonlyerp')
                    ->as_array();

                foreach($img_arrs as $arr){
                    if(stristr($arr['filename'],'-ALL')||stristr($arr['filename'],'-all')){
                        $thumbnail = $arr['filename'];
                        break;
                    }
                }

                if($thumbnail==''){
                    foreach($img_arrs as $arr){
                        if(stristr($arr['filename'],'http')){
                            $thumbnail = $arr['filename'];
                            break;
                        }
                    }
                }
            }
        }else{
            $thumbnail = $img_arrs['filename'];
        }
        //END      --------------------------------------CWY修改代码------------------------------------
        return $thumbnail;
    }
}
